/**
* For Video Page
*/

/**
* This function set video
* @author trungdt
* @since Oct 30, 2011
*/
function SetVideo(){
    jQuery("#thumbContainer li.current").removeClass("current");
    jQuery("#thumbContainer li[rel='"+VideoResponeObject.id+"']").css("display","list-item").addClass("current");

    jQuery("#splashImage").remove();
    jQuery("#mediaCenter_location").remove();
    jwplayer("mediaCenter_videoBox").remove();
    jwplayer("mediaCenter_videoBox").setup({
        flashplayer: flash_file,
        file: VideoResponeObject.file_path,
        height: 450,
        width: 600,
        autostart: true,
        image: VideoResponeObject.thumbnail,
        skin: skin_file,
        plugins: {
            sharing: { link: VideoResponeObject.url_link },
            'timeslidertooltipplugin-2': {}
        }
    });
    // show current on 
    jQuery("#thumbContainer")[0].scrollTo(".current");
    jQuery.scrollTo("#mediaMain",500);
}

/**
* This function play video by input video_name
* @author trungdt 
* @since Oct 27, 2011
*/
var VideoResponeObject=new Object;
VideoResponeObject.seo_name = "";
function PlayVideo(video_name){

    if (video_name == VideoResponeObject.seo_name)
        {
        SetVideo();
        return;
    }

    // first, search in playlist that this video is in playlist or  not
    window.location.hash="#"+video_name;
    // get video info before we play
    var temp=Math.random()*9999;
    jQuery.post("/index.php", {
        module: 'videomodule',
        action: 'video_getvideo',
        id:video_name,
        t: temp
    }, function(data){
        data=ajax_string_dim(data);
        if (data == "")
            return;
        else
        {
            VideoResponeObject = eval('(' + data + ')');

            // check if this already in playlist or we should load new playlist
            if (jQuery("#thumbContainer li[rel='"+VideoResponeObject.id+"']").length == 0)
            {
                // we should load new playlist
                playlist_should_loadVideo=true;
                PlaylistLoad(VideoResponeObject.cat_id);
            }
            else
                SetVideo();
        }		
    });
}

var playlist_should_loadVideo=false;
function PlaylistLoad(playlist_id){
    jQuery("#loader").fadeIn(); 
    jQuery("#thumbContainer ul").fadeOut("fast");
    var temp=Math.random()*9999;
    jQuery.post("/index.php", {
        module: 'videomodule',
        action: 'video_getvideo',
        playlist_id: playlist_id,
        t: temp
    }, function(data){
        
        // reload the playlist
        jQuery("#thumbContainer ul").html(data);
        InitScrollPane();
        
        //
        jQuery("#thumbContainer ul").fadeIn();
        jQuery("#loader").fadeOut();
        
        if (playlist_should_loadVideo)
            SetVideo();
        playlist_should_loadVideo=false;
        
        // active the button category
        jQuery("#mediaCategoryButtons").children(".mediaSelected").removeClass("mediaSelected");
        jQuery("#mediaCategoryButtons a[rel='"+playlist_id+"']").addClass("mediaSelected");
    });
}

function InitScrollPane(){
    jQuery("#thumbContainer").jScrollPane({scrollbarWidth: 15,scrollbarMargin: 2,animateTo: true,animateInterval: 50,animateStep: 5});

    jQuery("#thumbContainer ul li").hover(function(){
        jQuery(this).addClass("hover");
    }, function(){
        jQuery(this).removeClass("hover");
    })

    jQuery("#thumbContainer ul li a").click(function(){
        var video_id=jQuery(this).attr("rel");
        PlayVideo(video_id);
        return false;
    });
    
    var items= jQuery("#thumbContainer ul li").length;
    jQuery("#totalResults").html(items);
    jQuery("#totalResultNotFilter").html(items);
    
}

jQuery(document).ready(function(){
    // inital for hover
    jQuery(".mediaResultsGrid ol li a").hover(function(){
        jQuery(".playHolder").html('');
        jQuery(this).children(".playHolder").html('<div class="playHover"></div>');
    }, function(){
        jQuery(".playHolder").html('');
    });

    // for scroll
    InitScrollPane();

    // for every link video in sub cat
    jQuery(".mediaResultsGrid ol li a").click(function(){
        var video_id=jQuery(this).attr("rel");
        PlayVideo(video_id);
        return false;
    });

    // load video in hash
    var video_hash=window.location.hash;
    if (video_hash.length > 0)
        {
        video_hash=video_hash.substr(1);
        PlayVideo(video_hash);
    }
    
    // for category button
    jQuery("#mediaCategoryButtons a").click(function(){
        jQuery("#video_page_title").html(jQuery(this).html());
        var playlist_id = jQuery(this).attr("rel");
        PlaylistLoad(playlist_id);
        return false;
    });

    // search box
    jQuery("input#filter").keyup(function(){
        var input_val=jQuery(this).val().toLowerCase();
        var remain_count=0;
        jQuery("#thumbContainer ul").children("li").each(function(){
            if ((input_val=="") || jQuery(this).attr("rev").toLowerCase().indexOf(input_val) != -1)
                {
                remain_count++;
                if (jQuery(this).css("display")=="none")
                    jQuery(this).fadeIn("fast");
            }
            else
                {
                if (jQuery(this).css("display")=="list-item")
                    jQuery(this).fadeOut("fast");
            }
        });

        if (input_val == "")
            {
            jQuery("#totalResultNotFilterPanel").fadeOut();
            jQuery("#totalResults").html(jQuery("#totalResultNotFilter").html());
        }
        else
            {
            jQuery("#totalResultNotFilterPanel").fadeIn();
            jQuery("#totalResults").html(remain_count);
        }
    });
});